package com.wcf.mybatis.utils.plugins;

import org.mybatis.generator.api.GeneratedXmlFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.XmlConstants;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
import static org.mybatis.generator.internal.util.messages.Messages.getString;

/**
 * @author wuchaofei
 * @date 2016/3/28
 */
public class TableInfoPlugin extends PluginAdapter{

    private List<IntrospectedTable> introspectedTableList;

    public TableInfoPlugin() {
        introspectedTableList = new ArrayList<IntrospectedTable>();
    }

    public boolean validate(List<String> warnings) {
        boolean valid = true;

        if (!stringHasValue(properties
                .getProperty("targetProject"))) { //$NON-NLS-1$
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                    "SqlMapConfigPlugin", //$NON-NLS-1$
                    "targetProject")); //$NON-NLS-1$
            valid = false;
        }

        if (!stringHasValue(properties
                .getProperty("targetPackage"))) { //$NON-NLS-1$
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                    "SqlMapConfigPlugin", //$NON-NLS-1$
                    "targetPackage")); //$NON-NLS-1$
            valid = false;
        }

        return valid;
    }

    @Override
    public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() {
        /*Document document = new Document(
                XmlConstants.IBATIS2_SQL_MAP_CONFIG_PUBLIC_ID,
                XmlConstants.IBATIS2_SQL_MAP_CONFIG_SYSTEM_ID);*/
        Document document = new Document(
                XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID,
                XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);

        XmlElement root = new XmlElement("tables"); //$NON-NLS-1$
        document.setRootElement(root);
        root.addElement(new TextElement("<!--")); //$NON-NLS-1$
        root.addElement(new TextElement("  This file is generated by MyBatis Generator.")); //$NON-NLS-1$
        root.addElement(new TextElement("  This file is the shell of an SqlMapConfig file - in many cases you will need to add")); //$NON-NLS-1$
        root.addElement(new TextElement("    to this file before it is usable by iBATIS.")); //$NON-NLS-1$

        StringBuilder sb = new StringBuilder();
        sb.append("  This file was generated on "); //$NON-NLS-1$
        sb.append(new Date());
        sb.append('.');
        root.addElement(new TextElement(sb.toString()));
        root.addElement(new TextElement("-->"));

        XmlElement tableNode;
        for (IntrospectedTable table : introspectedTableList) {
            tableNode = new XmlElement("table");
            tableNode.addAttribute(new Attribute("name", table.getAliasedFullyQualifiedTableNameAtRuntime()));
            for (IntrospectedColumn col : table.getAllColumns()) {
                XmlElement columnElement = new XmlElement("column");
                tableNode.addElement(columnElement);
                columnElement.addAttribute(new Attribute("columnName", col.getActualColumnName()));
                columnElement.addAttribute(new Attribute("javaProperty", col.getJavaProperty()));
                columnElement.addAttribute(new Attribute("jdbcTypeName", col.getJdbcTypeName()));
                columnElement.addAttribute(new Attribute("javaTypeName", col.getFullyQualifiedJavaType().getFullyQualifiedName()));
            }
            root.addElement(tableNode);
        }

        GeneratedXmlFile gxf = new GeneratedXmlFile(document,
                properties.getProperty("fileName", "TableMap.xml"),
                properties.getProperty("targetPackage"),
                properties.getProperty("targetProject"),
                false, context.getXmlFormatter());

        List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>(1);
        answer.add(gxf);
        return answer;
    }

    @Override
    public boolean sqlMapGenerated(GeneratedXmlFile sqlMap,
                                   IntrospectedTable introspectedTable) {
        List<IntrospectedColumn> cols = introspectedTable.getAllColumns();
        /*for (IntrospectedColumn col : cols) {
            col.getActualColumnName();
            col.getJavaProperty();
            col.getJdbcTypeName();
            col.getFullyQualifiedJavaType();
        }*/
        introspectedTableList.add(introspectedTable);
        return true;
    }
}
